const fs = require("fs");
const path = require("path");
const readline = require("readline").createInterface({
  input: process.stdin,
  output: process.stdout,
});

const createMD = (fileName = "record") => {
  const one_day_ms = 24 * 60 * 60 * 1000; // 一天有多少毫秒

  const getDateLocaleString = (add = undefined) => {
    const ms = Date.now() + (add || 0);
    const date = new Date(ms);
    return date.toLocaleString().split(" ")[0];
  };

  const current_day = getDateLocaleString();

  const after_one_day = getDateLocaleString(one_day_ms);

  const after_three_day = getDateLocaleString(one_day_ms * 3);

  const after_six_day = getDateLocaleString(one_day_ms * 6);

  const after_fourteen_day = getDateLocaleString(one_day_ms * 14);

  const after_twenty_nine_day = getDateLocaleString(one_day_ms * 29);

  let tableHeader = `
- Y:完成
- N :未完成
  
单词 | 释义 | 音标 | 第一天(Y/N) | 第二天(Y/N) | 第四天(Y/N) | 第七天(Y/N) | 第十五天(Y/N) | 第三十天(Y/N)
---|---|---|---|---|---|---|---|---
| | | | ${current_day} | ${after_one_day} | ${after_three_day} | ${after_six_day}  | ${after_fourteen_day} | ${after_twenty_nine_day}
`;

  for (let index = 1; index <= 28; index++) {
    tableHeader += `| | | | | | | | | \n`
  }

  fs.writeFileSync(path.resolve(__dirname, `${fileName}.md`), tableHeader);
  console.log("创建成功");
  readline.close();
};

const loop = () => {
  readline.question("输入文件名称:", (answer) => {
    try {
      if (!answer) {
        throw Error("请输入文件名称");
      }
      const isHad = fs.existsSync(path.resolve(__dirname, `${answer}.md`));
      if (isHad) {
        console.log("已经存在，请重新输入！");
        loop();
      } else {
        createMD(answer);
      }
    } catch (error) {
      console.log(error);
      readline.close();
    }
  });
};

loop();
